<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 企业模块配置 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\model;

use think\Cache;
use think\Db;
use think\Model;

class EnterpriseModuleConfig {

    const CACHE_KEY_FIX = "core.EnterpriseModuleConfig.";

    /**
     * 获取指定配置项的值
     *
     * @param $eid
     * @param $mid
     * @param $key
     * @param bool $default
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getConfig($eid, $mid, $key, $default = false) {
        $enterpriseModuleConfigs = self::getConfigs($eid, $mid);
        if (!isset($enterpriseModuleConfigs[$key])) {
            return $default;
        }
        return $enterpriseModuleConfigs[$key]['value'];
    }

    /**
     * 更新配置
     *
     * @param $eid
     * @param $mid
     * @param $key
     * @param $val
     * @return int|string
     */
    public static function updateConfig($eid, $mid, $key, $val) {
        $ret = self::_q()->insertGetId([
            'enterprise_id' => $eid,
            'module_id' => $mid,
            'key' => $key,
            'value' => $val
        ], true);
        self::clearCache($eid, $mid);
        return $ret;
    }

    /**
     * 获取分组配置
     *
     * @param $eid
     * @param $mid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getGroupConfigs($eid, $mid) {
        $enterpriseModuleConfigs = self::getConfigs($eid, $mid);
        $enterpriseModuleConfigGroups = [];
        foreach ($enterpriseModuleConfigs as $key => $enterpriseModuleConfig) {
            $group = fetch($enterpriseModuleConfig, 'group');
            if (!isset($enterpriseModuleConfigGroups[$group])) {
                $enterpriseModuleConfigGroups[$group] = [];
            }
            $enterpriseModuleConfigGroups[$group][$key] = $enterpriseModuleConfig;
        }
        return $enterpriseModuleConfigGroups;
    }

    /**
     * 清除缓存
     *
     * @param $eid
     * @param $mid
     */
    public static function clearCache($eid, $mid) {
        $cacheKey = self::CACHE_KEY_FIX . $eid . "." . $mid;
        Cache::rm($cacheKey);
    }

    /**
     * 获取模块的配置项
     *
     * @param $eid
     * @param $mid
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getConfigs($eid, $mid) {
        $cacheKey = self::CACHE_KEY_FIX . $eid . "." . $mid;
        $enterpriseModuleConfigs = Cache::get($cacheKey);
        if ($enterpriseModuleConfigs !== false) {
            return $enterpriseModuleConfigs;
        }

        // 模块定义的config
        $moduleConfigs = SiteModuleConfig::getConfigs($mid);
        $moduleConfigs = remap($moduleConfigs, "key");

        // 当前企业对config设置的值
        $enterpriseModuleConfigs = self::_q()->where([
            'enterprise_id' => $eid,
            'module_id' => $mid
        ])->select();
        $enterpriseModuleConfigs = remap($enterpriseModuleConfigs, "key");

        foreach ($moduleConfigs as &$moduleConfig) {
            $key = $moduleConfig['key'];

            if (!isset($enterpriseModuleConfigs[$key])) {
                $enterpriseModuleConfigs[$key] = [
                    'value' => $moduleConfig['defaultval']
                ];
            }
            $enterpriseModuleConfigs[$key]['title'] = $moduleConfig['title'];
            $enterpriseModuleConfigs[$key]['type'] = $moduleConfig['type'];
            $enterpriseModuleConfigs[$key]['tip'] = $moduleConfig['tip'];
            $enterpriseModuleConfigs[$key]['group'] = $moduleConfig['group'];
            if (!empty($moduleConfig['param'])) {
                $enterpriseModuleConfigs[$key]['param'] = json_decode($moduleConfig['param'], true);
            }
        }

        Cache::set($cacheKey, $enterpriseModuleConfigs);
        return $enterpriseModuleConfigs;
    }

    /**
     * 删除模块相关的所有设置
     *
     * @param $mid
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function deleteByMid($mid) {
        return self::_q()->where(['module_id' => $mid])->delete();
    }

    /**
     * @return \think\db\Query
     */
    private static function _q() {
        return Db::name("EnterpriseModuleConfig");
    }
}